package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.dao.SolutionMapper;
import com.homedo.odin.solution.dao.UserSolutionMapper;
import com.homedo.odin.solution.enumcode.basic.CollectionEnum;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.enumcode.basic.DownloadCollectEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.dto.SolutionDownloadResponseBean;
import com.homedo.odin.solution.model.dto.UserSolutionResult;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.UserSolution;
import com.homedo.odin.solution.model.request.pc.collect.CollectSolutionRequestBean;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p> 用户下载收藏关系表 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class UserSolutionServiceImpl extends BaseService<UserSolutionMapper, UserSolution> {

    @Autowired
    private SolutionMapper solutionMapper;

    //*******************张磊Start***********************

    /**
     * PC_收藏/取消收藏 方案
     */
    @Transactional(rollbackFor = Exception.class)
    public void collectSolution(CollectSolutionRequestBean requestBean) {
        Solution solution = solutionMapper.getSolution(requestBean.getSolutionId());
        if (solution == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该方案不存在");
        }
        if (!SolutionStateEnum.SHELVE.getDbValue().equals(solution.getStatus())) {
            throw new BizException(Meta.CODE_451.getCode(), "该方案不是上架中");
        }

        UserSolution userSolution = baseMapper.getUserSolution(requestBean.getUserId(), requestBean.getSolutionId(),
                DownloadCollectEnum.COLLECTION.getDbValue());

        //收藏方案
        if (CollectionEnum.COLLECTION.getDbValue().equals(requestBean.getStatus())) {
            if (userSolution == null) {
                UserSolution insertData = new UserSolution();
                insertData.setActionType(DownloadCollectEnum.COLLECTION.getDbValue());
                insertData.setCreateTime(new Date());
                insertData.setLastModifiedTime(new Date());
                insertData.setSolutionId(requestBean.getSolutionId());
                insertData.setUserId(requestBean.getUserId());
                baseMapper.insert(insertData);
            } else if (DeleteEnum.IS_DEL.getDbValue().equals(userSolution.getCollectionStatus())) {
                userSolution.setCollectionStatus(DeleteEnum.NOT_DEL.getDbValue());
                baseMapper.updateById(userSolution);
            }
            //方案收藏量
            solutionMapper.updateSolutionCollect(requestBean.getSolutionId(), 1);
        } else {
            //删除收藏方案
            if (userSolution == null) {
                throw new BizException(Meta.CODE_451.getCode(), "没有对应的收藏记录");
            }
            if (DeleteEnum.IS_DEL.getDbValue().equals(userSolution.getCollectionStatus())) {
                throw new BizException(Meta.CODE_451.getCode(), "该记录已删除");
            }
            userSolution.setCollectionStatus(DeleteEnum.IS_DEL.getDbValue());
            baseMapper.updateById(userSolution);
            solutionMapper.updateSolutionCollect(requestBean.getSolutionId(), -1);
        }
    }

    //*******************张磊End***********************

    /**
     * 查找方案收藏、下载动态列表
     */
    public List<SolutionDownloadResponseBean> solutions(Integer actionType) {
        return baseMapper.solutions(actionType);
    }

    public List<UserSolution> getUserSolutionList(Integer userId, int actionType) {
        return baseMapper.getUserSolutionList(userId, actionType);
    }

    public UserSolution getUserSolution(Integer userId, Long solutionId, Integer actionType) {
        return baseMapper.getUserSolution(userId, solutionId, actionType);

    }

    public void addUserSolution(UserSolution uSolution) {
        baseMapper.insert(uSolution);
    }

    public List<UserSolutionResult> querySdownloadList(Integer userId, Integer userSolutionType, String solutionName,
            Integer solutionStatus, Date createStartTime, Date createEndTime) {
        return baseMapper.querySdownloadList(userId,
                userSolutionType, solutionName, solutionStatus, createStartTime, createEndTime);
    }

    public PageInfo<UserSolutionResult> querySdownloadList(Integer uid, Integer userSolutionType, String solutionName,
            Integer solutionStatus, Date createStartTime, Date createEndTime, PageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        return new PageInfo<UserSolutionResult>(baseMapper.querySdownloadList(uid,
                userSolutionType, solutionName, solutionStatus, createStartTime, createEndTime));
    }

    public void updateUserSolution(UserSolution userSolution) {
        baseMapper.updateById(userSolution);
    }

}
